// 组件赋能 ref获取实例

import React from 'react';
import { Button } from 'antd';

function Hoc(Component: any) {
  return class WrapComponent extends React.Component<any, any> {
    node: any;
    constructor(props: any) {
      super(props);
      /* 获取实例，可以做一些其他的操作。 */
      this.node = null;
    }

    componentDidMount(): void {
      console.log(this.node);
    }

    handleClick = () => {
      this.node.handleClick();
    };

    render() {
      return (
        <>
          <Button type="primary" onClick={this.handleClick}>
            onclick
          </Button>
          <Component {...this.props} ref={(node: any) => (this.node = node)} />
        </>
      );
    }
  };
}

class Index extends React.Component {
  handleClick = () => {
    console.log('handleClick');
  };
  render() {
    return <p>index</p>;
  }
}

export default Hoc(Index);
